package com.ican.nc;

import java.net.URL;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ican.common.SystemConstants;
import com.ican.domain.product.Product;
import com.ican.service.product.ProductService;
import com.ican.util.SpringContextUtils;

public class RuleProductDetailScan implements Runnable {
    private final static Logger logger = LoggerFactory.getLogger(RuleProductDetailScan.class);
    
    private Integer proId;
    private ParseRuleVo rule;
    
    public RuleProductDetailScan(Integer proId, ParseRuleVo rule){
        this.proId = proId;
        this.rule = rule;
    }
        
    public void run() {
        ProductService productService = null;
        String result = null;
        TagNode tagNode = null;
        HtmlCleaner html = new HtmlCleaner();
        try {
            productService = (ProductService) SpringContextUtils.getContext().getBean("productService");
            Product product = productService.findProduct(proId);
            if(product.getNcStatus() == SystemConstants.NC_STATUS_RUN){
                return;
            }
            productService.updateProductNcStatus(proId, SystemConstants.NC_STATUS_RUN);
            String url = product.getUrl();
            tagNode = html.clean(new URL(url));
            //名称
            result = ParseUtil.loadCommon(tagNode, rule.getProductName());
            product.setName(result);
            //编码
            result = ParseUtil.loadCommon(tagNode, rule.getProductCode());
            if(result == null || result.length() < 1){
                result = url;
                result = ParseUtil.parseResult(result, rule.getProductCode());
            }
            product.setCode(result);
            //价格
            result = ParseUtil.loadCommon(tagNode, rule.getProductPrice());
            product.setPrice(result);
            result = ParseUtil.loadCommon(tagNode, rule.getProductPriceUnit());
            if(result == null || result.length() < 1){
                result = "$";
            }
            product.setPriceUnit(result);
            //重量;
            result = ParseUtil.loadCommon(tagNode, rule.getProductWeight());
            product.setWeight(result);
            result = ParseUtil.loadCommon(tagNode, rule.getProductWeightUnit());
            product.setWeightUnit(result);
            //描述
            result = ParseUtil.tagNodeHtml(html, tagNode, rule.getProductDescription());
            product.setDescription(result);
            productService.updateProduct(product);

            boolean bo1 = ParseUtil.loadImage(tagNode, rule.getProductImage(), product.getId(), productService);

            boolean bo2 = ParseUtil.loadAttr(tagNode, rule.getProductAttribute(), rule.getProductAttributeValue(), product.getId(), product.getLangId(), productService);
            
            if(bo1 && bo2){
                productService.updateProductNcStatus(proId, SystemConstants.NC_STATUS_SUCCESS);
            }else{
                productService.updateProductNcStatus(proId, SystemConstants.NC_STATUS_FAILED);
            }
        } catch (Exception e) {
            productService.updateProductNcStatus(proId, SystemConstants.NC_STATUS_FAILED);
            logger.error(e.getMessage(), e);
        }
    }
}
